package com.guide.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.guide.bizdto.TravelTeamSel;
import com.guide.dao.TravelTeamDao;
import com.guide.domain.TravelTeam;
import com.guide.domain.TravelTeamQuery;
import com.guide.domain.TravelTeamQuery.Criteria;
import com.guide.persistence.GuideApplyMapper;
import com.guide.persistence.TravelTeamMapper;
import com.guide.utils.ConstantUtils;
@Repository("travelorgTeam")
public class TravelTeamDaoImpl implements TravelTeamDao{

	
	@Autowired
	private TravelTeamMapper travelTeamMapper;
	
	@Autowired
	private GuideApplyMapper guideApplyMapper;
	
	@Override
	public long insertTravelTeam(TravelTeam travelTeam) {
		// TODO Auto-generated method stub
		return travelTeamMapper.insert(travelTeam);
	}

	@Override
	public List<TravelTeam> getTravelTeamList(Long userId,TravelTeamSel travelTeamSel) {
		// TODO Auto-generated method stub
		TravelTeamQuery query = new TravelTeamQuery();
		query.setOrderByClause("startTime desc, id desc");
		Criteria  criteria =query.createCriteria().andUserIdEqualTo(userId);
		if(travelTeamSel.getApply()==1){
			criteria.andApplycountEqualTo(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}else if(travelTeamSel.getApply()==2) {
			criteria.andApplycountGreaterThan(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}
		if(travelTeamSel.getDaidui()==1){
			criteria.andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		if(travelTeamSel.getStartTime()==1) {
			criteria.andStartTimeLessThan(new Date());
		}
		if(travelTeamSel.getStartTime()==2) {
			criteria.andStartTimeGreaterThan(new Date());
		}
		if(!StringUtils.isEmpty(travelTeamSel.getCity())){
			criteria.andSourcecityEqualTo(travelTeamSel.getCity());
		}
		
		Criteria  criteria2 =query.createCriteria().andUserIdEqualTo(userId);
		if(travelTeamSel.getApply()==1){
			criteria2.andApplycountEqualTo(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}else if(travelTeamSel.getApply()==2) {
			criteria2.andApplycountGreaterThan(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}
		if(travelTeamSel.getDaidui()==1){
			criteria2.andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		if(travelTeamSel.getStartTime()==1) {
			criteria2.andStartTimeLessThan(new Date());
		}
		if(travelTeamSel.getStartTime()==2) {
			criteria2.andStartTimeGreaterThan(new Date());
		}
		if(!StringUtils.isEmpty(travelTeamSel.getCity())){
			criteria2.andTargetcityEqualTo(travelTeamSel.getCity());
		}
		
		query.or(criteria2);
		
		return travelTeamMapper.selectByExampleWithBLOBs(query, travelTeamSel.getPageIndex()*travelTeamSel.getPageSize(), travelTeamSel.getPageSize());
	}

	@Override
	public Integer countTravelTeamList(Long userId,TravelTeamSel travelTeamSel) {
		TravelTeamQuery query = new TravelTeamQuery();
		Criteria  criteria = query.createCriteria().andUserIdEqualTo(userId);
		if(travelTeamSel.getApply()==1){
			criteria.andApplycountEqualTo(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}else if(travelTeamSel.getApply()==2) {
			criteria.andApplycountGreaterThan(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}
		if(travelTeamSel.getDaidui()==1){
			criteria.andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		if(travelTeamSel.getStartTime()==1) {
			criteria.andStartTimeLessThan(new Date());
		}
		if(travelTeamSel.getStartTime()==2) {
			criteria.andStartTimeGreaterThan(new Date());
		}
		if(!StringUtils.isEmpty(travelTeamSel.getCity())){
			criteria.andSourcecityEqualTo(travelTeamSel.getCity());
		}
		if(!StringUtils.isEmpty(travelTeamSel.getCity())){
			criteria.andSourcecityEqualTo(travelTeamSel.getCity());
		}
		Criteria  criteria2 =query.createCriteria().andUserIdEqualTo(userId);
		if(travelTeamSel.getApply()==1){
			criteria2.andApplycountEqualTo(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}else if(travelTeamSel.getApply()==2) {
			criteria2.andApplycountGreaterThan(0).andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0);//大于0代表有申请
		}
		if(travelTeamSel.getDaidui()==1){
			criteria2.andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		if(travelTeamSel.getStartTime()==1) {
			criteria2.andStartTimeLessThan(new Date());
		}
		if(travelTeamSel.getStartTime()==2) {
			criteria2.andStartTimeGreaterThan(new Date());
		}
		if(!StringUtils.isEmpty(travelTeamSel.getCity())){
			criteria2.andTargetcityEqualTo(travelTeamSel.getCity());
		}
		
		query.or(criteria2);
		return travelTeamMapper.countByExample(query);
	}

	@Override
	public TravelTeam getTravelTeamById(Long id) {
		// TODO Auto-generated method stub
		return travelTeamMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TravelTeam> getNewTravelTeamList(TravelTeamSel travelTeamSel) {
		TravelTeamQuery query = new TravelTeamQuery();
		query.createCriteria().andGuidestatusEqualTo(0);
		query.setOrderByClause("startTime desc, id desc");
		return travelTeamMapper.selectByExampleWithBLOBs(query, travelTeamSel.getPageIndex()*travelTeamSel.getPageSize(), travelTeamSel.getPageSize());
	}

	@Override
	public Integer countNewTravelTeamList(TravelTeamSel travelTeamSel) {
		TravelTeamQuery query = new TravelTeamQuery();
		query.createCriteria().andGuidestatusEqualTo(0);
		return travelTeamMapper.countByExample(query);
	}

	@Override
	public void updateTravelTeam(TravelTeam travelTeam) {
		// TODO Auto-generated method stub
		travelTeamMapper.updateByPrimaryKeySelective(travelTeam);
	}

	@Override
	public List<TravelTeam> getTravelTeamListByStatus(Long userId,TravelTeamSel travelTeamSel) {
		// TODO Auto-generated method stub
/*		TravelTeamQuery query = new TravelTeamQuery();
		GuideApplyQuery query1 = new GuideApplyQuery();
		com.guide.domain.GuideApplyQuery.Criteria criteria1 = query1.createCriteria().andUserIdEqualTo(userId);
		if(daidui){
			criteria1.andStatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		List<GuideApply> guideApplys = guideApplyMapper.selectByExample(query1,pageIndex*pageSize, pageSize);
		List<Long> teamIds = new ArrayList<Long>();
		if(!CollectionUtils.isEmpty(guideApplys)){
			for(GuideApply guideApply:guideApplys) {
				teamIds.add(guideApply.getTeamId());
			}
		}
		query.setOrderByClause("startTime desc,id desc");
		query.createCriteria().andIdIn(teamIds);
		return travelTeamMapper.selectByExampleWithBLOBs(query, null, null);*/
		Integer guidestatus = null;
		Integer commentstatus = null;
		Integer status = null;
		if(travelTeamSel.getDaidui()==1) {
			guidestatus = ConstantUtils.GUIDE_STATUS_1;
		}else if(travelTeamSel.getDaidui()==2) {
			guidestatus = ConstantUtils.GUIDE_STATUS_0;
		}else if(travelTeamSel.getDaidui()==3) {
			guidestatus = ConstantUtils.GUIDE_STATUS_1;
			status =  ConstantUtils.GUIDE_STATUS_1;;
		}
		return travelTeamMapper.selectByExampleWithBLOBsByStatus(guidestatus, commentstatus, travelTeamSel.getStartTime(),status, userId, travelTeamSel.getPageIndex()*travelTeamSel.getPageSize(), travelTeamSel.getPageSize());
	}

	@Override
	public Integer countTravelTeamListByStatus(Long userId,TravelTeamSel travelTeamSel) {
	/*	GuideApplyQuery query1 = new GuideApplyQuery();
		com.guide.domain.GuideApplyQuery.Criteria criteria1 = query1.createCriteria().andUserIdEqualTo(userId);
		if(daidui){
			criteria1.andStatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		}
		return guideApplyMapper.countByExample(query1);*/
		Integer guidestatus = null;
		Integer commentstatus = null;
		Integer status = null;
		if(travelTeamSel.getDaidui()==1) {
			guidestatus = ConstantUtils.GUIDE_STATUS_1;
		}else if(travelTeamSel.getDaidui()==2) {
			guidestatus = ConstantUtils.GUIDE_STATUS_0;
		}else if(travelTeamSel.getDaidui()==3) {
			guidestatus = ConstantUtils.GUIDE_STATUS_1;
			status =  ConstantUtils.GUIDE_STATUS_1;;
		}
		return travelTeamMapper.countByExampleByStatus(guidestatus, commentstatus, travelTeamSel.getStartTime(),status, userId);
	}

	@Override
	public void updateByPrimaryKeySelective(TravelTeam travelTeam) {
		// TODO Auto-generated method stub
		travelTeamMapper.updateByPrimaryKeySelective(travelTeam);
	}

	@Override
	public void removeTravelTeam(Long id) {
		// TODO Auto-generated method stub
		travelTeamMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<TravelTeam> getNewTravelTeamListByGuide(String city,TravelTeamSel travelTeamSel, List<Long> teamIds) {
		// TODO Auto-generated method stub
		TravelTeamQuery query = new TravelTeamQuery();
		query.createCriteria().andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0).andSourcecityEqualTo(city).andStartTimeGreaterThan(new Date()).andIdNotIn(teamIds);//.andGuidestatusEqualTo(0);
		
		TravelTeamQuery query1 = new TravelTeamQuery();
		Criteria c = query1.createCriteria().andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0).andTargetcityEqualTo(city).andStartTimeGreaterThan(new Date()).andIdNotIn(teamIds);//.andGuidestatusEqualTo(0);

		query.or(c);
		query.setOrderByClause("startTime desc, id desc");
		return travelTeamMapper.selectByExampleWithBLOBs(query, travelTeamSel.getPageIndex()*travelTeamSel.getPageSize(), travelTeamSel.getPageSize());
	}

	@Override
	public Integer countNewTravelTeamListByGuide(String city,TravelTeamSel travelTeamSel, List<Long> teamIds) {
		TravelTeamQuery query = new TravelTeamQuery();
		query.createCriteria().andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0).andSourcecityEqualTo(city).andStartTimeGreaterThan(new Date()).andIdNotIn(teamIds);//.andGuidestatusEqualTo(0);
		
		TravelTeamQuery query1 = new TravelTeamQuery();
		Criteria c = query1.createCriteria().andGuidestatusEqualTo(ConstantUtils.GUIDE_STATUS_0).andTargetcityEqualTo(city).andStartTimeGreaterThan(new Date()).andIdNotIn(teamIds);//.andGuidestatusEqualTo(0);

		query.or(c);
		return travelTeamMapper.countByExample(query);
	}

	@Override
	public void updateCommentStatusByPrimaryKeySelective(TravelTeam travelTeam) {
		// TODO Auto-generated method stub
		travelTeamMapper.updateCommentStatusByPrimaryKeySelective(travelTeam);
	}



	
	
	

	
}
